import json
import re
import stomp
from entity.MQHead import MQHead
from entity.MQMessage import MQMessage
from unitys.MyEncoder import MyEncoder
from datetime import datetime

activemq_ip = "10.198.12.131"
activemq_port = "61613"
activemq_user = "admin"
activemq_pass = "o6inC6CXv5!FEaq"
activemq_name = "videoWarningOutMq"


def send_mq_one(data):
    conn = stomp.Connection([(activemq_ip, activemq_port)])  # ActiveMQ默认端口为61613
    conn.connect(activemq_user, activemq_pass, wait=True)  # 替换为实际的用户名和密码
    conn.send(getMQMessage(data), destination=activemq_name, content_type='text/plain')
    # 关闭连接
    conn.disconnect()


def send_mq_multiple(dataList):
    conn = stomp.StompConnection10([(activemq_ip, activemq_port)], auto_content_length=False)  # ActiveMQ默认端口为61613

    conn.connect(activemq_user, activemq_pass, wait=True)  # 替换为实际的用户名和密码
    for data in dataList:
        conn.send(body=getMQMessage(data), destination=activemq_name, content_type='text/plain')
    # 关闭连接
    conn.disconnect()


def sendMessage(message):
    conn = stomp.StompConnection10([(activemq_ip, activemq_port)], auto_content_length=False)  # ActiveMQ默认端口为61613
    conn.connect(activemq_user, activemq_pass, wait=True)  # 替换为实际的用户名和密码
    conn.send(body=message, destination=activemq_name, content_type='text/plain')
    # 关闭连接
    conn.disconnect()


def getMQMessage(data):
    # jsons = {
    #     "Head": {
    #         "ContentLength": 1,
    #         "ContentType": "text/plain",
    #         "Date": "2024-07-09 21:52:06",
    #         "ApplicationType": "GWT_Video"
    #     },
    #     "Body": r'{"AlarmTime": "%s", "DeviceId": "%s", "DeviceName": "%s", "AlarmImageName": "%s", "AlarmImageUrl": "%s", "AlarmVideoUrl": "%s", "AlarmClassId": "%s", "AlarmTargetNum": %s}' % (
    #         data.AlarmTime, data.DeviceId, data.DeviceName, data.AlarmImageName, data.AlarmImageUrl, data.AlarmVideoUrl,
    #         data.AlarmClassId,
    #         data.AlarmTargetNum)
    # }

    jsons = {
        "Head": {
            "ContentLength": 1,
            "ContentType": "text/plain",
            "Date": "2024-07-09 21:52:06",
            "ApplicationType": "GWT_Video"
        },
        "Body": r'{"alarmTime": "%s", "deviceId": "%s", "deviceName": "%s", "alarmImageName": "%s", "alarmImageUrl": "%s", "alarmVideoUrl": "%s", "alarmClassId": "%s", "alarmTargetNum": %s}' % (
            data.AlarmTime, data.DeviceId, data.DeviceName, data.AlarmImageName, data.AlarmImageUrl, data.AlarmVideoUrl,
            data.AlarmClassId,
            data.AlarmTargetNum)
    }
    return json.dumps(jsons, ensure_ascii=False)


def parseObjToJson(yourObj):
    return yourObj.__dict__.__str__().replace("\'", "\"")
